package com.lbx.dao;

import com.lbx.entity.User;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吕兵星
 * @date 2020/10/26 21:11
 */
@Component
public class UserDao {
    private static Integer userId=15;
    static List<User> users =null;
    static {
        User user1 = new User(1,"1aa",11,"1asd1234",0,"111@qq.com");
        User user2 = new User(2,"2aa",14,"2asd1234",1,"222@qq.com");
        User user3 = new User(3,"3aa",17,"3asd1234",1,"333@qq.com");
        User user4 = new User(4,"4aa",8,"4asd1234",0,"444@qq.com");
        User user5 = new User(5,"5aa",9,"5asd1234",1,"555@qq.com");
        User user6 = new User(6,"6aa",15,"6asd1234",1,"666@qq.com");
        User user7 = new User(7,"7aa",12,"7asd1234",0,"777@qq.com");
        User user8 = new User(8,"8aa",11,"1asd1234",0,"111@qq.com");
        User user9 = new User(9,"9aa",14,"2asd1234",1,"222@qq.com");
        User user10 = new User(10,"10aa",17,"3asd1234",1,"333@qq.com");
        User user11 = new User(11,"11aa",8,"4asd1234",0,"444@qq.com");
        User user12 = new User(12,"12aa",9,"5asd1234",1,"555@qq.com");
        User user13 = new User(13,"13aa",15,"6asd1234",1,"666@qq.com");
        User user14 = new User(14,"14aa",12,"7asd1234",0,"777@qq.com");

        users = new ArrayList<>();
       users.add(user1);users.add(user2);users.add(user3);users.add(user4);
       users.add(user5);users.add(user6);users.add(user7);
        users.add(user8);users.add(user9);users.add(user10);users.add(user11);
        users.add(user12);users.add(user13);users.add(user14);
    }
    public static Integer initId(){
        return userId++;
    }
    public List<User> getUsers(){
        return users;
    }

    public User getUserById(Integer id){
        for (User user : users) {
            if (user.getId().equals(id)){
                return user;
            }
        }
        return null;
    }

    public void updateUser(User user){
        users.remove(user.getId().intValue()-1);
        User user1 = new User();
        user1.setId(user.getId());
        user1.setName(user.getName());
        user1.setAge(user.getAge());
        user1.setPassword(user.getPassword());
        user1.setSex(user.getSex());
        user1.setEmail(user.getEmail());
        users.add(user.getId().intValue()-1,user1);

    }
    public void delete(int id){
        users.remove(getUserById(id));
    }
    public void add(User user){
        user.setId(initId());
        users.add(user);
    }
}
